<?php

namespace app\admin\service;

use app\model\Activity;
use app\model\ActivityStatistics;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\helper\Arr;
use think\Paginator;
use app\model\Applet;

class ActivityService
{
    /**
     * 获取列表数据
     * @throws DbException
     */
    public static function getList($params): Paginator
    {
        $where = [];
        if (!empty($params['name'])) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        $data = Activity::getPagingDataByWhere($where);
        $data->each(function (&$item){
            $item['type_text'] = Arr::get(Activity::TYPE_ARR,$item->type);
            $item['status_text'] = Arr::get(Activity::STATUS_ARR,$item->status);
            $sceneTextArr = collect($item->scene)->each(function ($sc){
                return Activity::SCENE_ARR[$sc];
            })->toArray();
            $item['scene_text'] = join('、',$sceneTextArr);
            $item['currency'] = intval(strval($item->recharge_price * 100));
        });
        return $data;
    }

    /**
     * 保存数据
     * @param $params
     * @return false|void
     */
    public static function saveData($params)
    {
        if (!empty($params['applet_ids'])) {
            $params['applet_ids'] = array_values(explode(',',$params['applet_ids']));
        }
        if (!empty($params['scene'])) {
            $params['scene'] = array_values($params['scene']);
        }
        $params['sum_currency'] = bcadd($params['send_currency'],$params['recharge_price'] * 100);
        unset($params['file']);
        $id = Activity::edit($params);

        if (!$id) {
            return false;
        }
    }

    /**
     * 获取活动信息
     * @param $id
     * @return Activity|array|mixed|\think\Model
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public static function getInfo($id)
    {
        $info = Activity::where('id',$id)->find();
        $info['applet_ids'] = collect($info['applet_ids'])->toArray();
        $info['scene'] = collect($info['scene'])->toArray();
        return $info;
    }

    /**
     * 统计列表
     * @param $params
     * @return Paginator
     * @throws DbException
     */
    public static function getStatisticsList($params): Paginator
    {
        $where = [];
        if (!empty($params['activity_id'])) {
            $where[] = ['activity_id', '=', $params['activity_id']];
        }
        if (!empty($params['applet_id'])) {
            $where[] = ['applet_id', '=', $params['applet_id']];
        }
        if (!empty($params['date'])) {
            $where[] = ['date', '=', $params['date']];
        }

        // 平台类型
        if (!empty($params['platform_type'])) {
            $appletModel = new Applet();
            $appletData = $appletModel->getDataByWhere(['type' => $params['platform_type']], 'id');
            $appletIds = array_column($appletData, 'id');
            $where[] = ['applet_id', 'in', $appletIds];
        }

        $list = ActivityStatistics::getPagingDataByWhere($where);
        $list->each(function (&$item) {
            $item['activity_name'] = $item->activity->name ?? '';
            $item['activity_type'] = isset($item->activity->type) ? Activity::TYPE_ARR[$item->activity->type] : '';
            // 充值金额/点击人数
            $item['arpu'] = $item->recharge_amount > 0 ? round($item->recharge_amount/$item->click_uv,2) : 0;
            // 小程序名称
            $item['applet_name'] = $item->applet->name ?? '';
        });
        return $list;
    }
}